<?php
class TipsController extends AppController {

	var $name = 'Tips';
	var $components = array('Email' );
	var $uses = array('Tip');
	//var $helpers = array('Cache');
	var $paginate = array(
		'limit' => 10,
		'order' => array('Tip.created' => 'desc'),
		'conditions' => array('Tip.approved' => 1)
	);
	var $cacheAction = array(
	    'index'  => 3600,
	'view' => 3600,
	);
	
	function beforeFilter() {
		parent::beforeFilter(); 
		//$this->Auth->allow('add', 'edit');
		if($this->is_admin())
			$this->paginate['conditions'] = null; //conditions
	}
	
	function _checkAccess($id) {
		$tip = $this->Tip->read(null, $id);
		//debug($this->Auth);
		if($this->is_admin())
			return true;
		if($tip['Tip']['ip'] != $_SERVER['REMOTE_ADDR']) {
			$this->Session->setFlash(__("You don't have access to this tip!", true));
			$this->redirect(array('action'=>'index'));
		}
		return true;
	}
	
	
	function admin_approve($id = null, $val = 1) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Tip.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Tip->id = $id;
		if($this->Tip->saveField('approved', $val)) {
			
			if($val == 1) {
				$message = __('Your new tip has been approved and can be viewed here:', true);
				$this->_simplemail($id, $message);
			}
			$this->Session->setFlash(__('Tip updated!', true));
			$this->redirect(array('action'=>'index'));
		} else {
			$this->Session->setFlash(__('Tip could not be updated!', true));
			$this->redirect(array('action'=>'index'));
		}
		
	}
	
	function _simplemail($id, $message = "") {
		// Send mail
		$tip = $this->Tip->read(null, $id);
		
		$mail_settings = Configure::read('Mail');
		$this->Email->from    = $mail_settings['from_name'] . ' <' . $mail_settings['from_mail'] . '>';
		$this->Email->to = $tip['Tip']['email'];
		$this->Email->cc = $mail_settings['admin_mail'];
		$this->Email->subject = __('Message from Surdeig.no', true);
		$body = $message . '\n' . $this->_urlfortip($tip);
		$this->Email->send($body);
		
		//Check if get run
		//$this->Tip->id = $id;
		//$this->Tip->saveField('body', $message);
		//App::import('Controller', 'Main'); 
		//$main = new MainController;
		//$main->simplemail($to, $subject, $body);
	}
	
	function _urlfortip($tip) {
		//$tip = $this->Tip->read(null, $id);
		$site_settings = Configure::read('Site');
		return $site_settings['site_url'] . '/tips/view/' . $tip['Tip']['id'];
	}
	
	function _cleaninput($tip) {
		$data = $tip;
		App::import('Sanitize');
		//Sanitize::paranoid($badString); $sanitize->clean($data); 
		//$data['Tip']['body'] = Sanitize::paranoid($data['Tip']['body'], array(' ', '*', '_', '-', '/',':', '/n','.', ',', '!'));
		$data['Tip']['name'] = Sanitize::html($data['Tip']['name'], true);
		//$data['Tip']['email'] = Sanitize::paranoid($data['Tip']['email'], array(' ', '*', '_', '-', '/',':'));
		return $data;
	}

	function index() {
		$this->Tip->recursive = 0;
		$this->set('tips', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Tip.', true));
			$this->redirect(array('action'=>'index'));
		}
		$tip = $this->Tip->read(null, $id);
		if( !$tip['Tip']['approved'] and !$this->is_admin()) {
			$this->Session->setFlash(__("You don't have access to this tip!", true));
			$this->redirect(array('action'=>'index'));
		}
		if(!$this->is_admin()) {
			// Update view count
			$this->Tip->id = $tip['Tip']['id'];
			$current_views = $tip['Tip']['views']  = $tip['Tip']['views'] + 1;
			$this->Tip->saveField('views', $current_views);
		}
		
		$this->set('tip', $tip);
		$this->set('referer', $this->referer('/tip/', true));
	}

	function add() {
		$this->Session->setFlash(__('The Tip could not be saved. Please, try again.', true));
		$this->redirect(array('action'=>'index'));
		if (!empty($this->data)) {
			
			$this->Tip->create();
			$this->data = $this->_cleaninput($this->data);
			// Set the ip in case it has been faked
			$tip_settings = Configure::read('Tip');
			$this->data['Tip']['ip'] = $_SERVER['REMOTE_ADDR'];
			$this->data['Tip']['approved'] = $tip_settings['default_approved'];
			// Todo: wash input
			if ($this->Tip->save($this->data)) {
				$this->Session->setFlash(__('The Tip has been saved.', true));
				$id = $this->Tip->getInsertID();
				$message = __('Your new tip has been saved and can be viewed here:', true);
				$this->_simplemail($id, $message);
				
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Tip could not be saved. Please, try again.', true));
			}
		} else {
			$this->data['Tip']['ip'] = $_SERVER['REMOTE_ADDR'];
		}
	}
	
	

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Tip', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			$this->_checkAccess($id);
			$this->data = $this->_cleaninput($this->data);
			// Todo: wash input
			if ($this->Tip->save($this->data)) {
				$this->Session->setFlash(__('The Tip has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Tip could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->_checkAccess($id);
			$this->data = $this->Tip->read(null, $id);
		}
	}

	function admin_index() {
		$this->Tip->recursive = 0;
		$this->set('tips', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Tip.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('tip', $this->Tip->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Tip->create();
			if ($this->Tip->save($this->data)) {
				$this->Session->setFlash(__('The Tip has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Tip could not be saved. Please, try again.', true));
			}
		}
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Tip', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Tip->save($this->data)) {
				$this->Session->setFlash(__('The Tip has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Tip could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Tip->read(null, $id);
		}
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Tip', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Tip->del($id)) {
			$this->Session->setFlash(__('Tip deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}
	

}
?>